/*
* 主订单相关
*
* */




/*----------------添加协单人------------------------------*/
var assistIndex;
function addAssist(tableName,scProductId) {
    var id = null;
    if(!isEmpty(tableName)){
        //获取选中行对象
        var trs = getChecked("#" + tableName);
        if (trs.length > 1) {
            dggui.dialog.errors('只能选择一个订单')
            return;
        }
        if (trs.length == 0) {
            dggui.dialog.errors('请选择一条数据');
            return;
        }
        id =trs[0].id;
    }else{
        if(isEmpty(scProductId)){
            dggui.dialog.errors('生产单ID不能为空');
            return;
        }
        id = scProductId;
    }
    dggui.dialog.loading();
    $.ajax({
        type: 'POST',
        async: false,
        url: sysInfo.basePath + "/rzsc/processing_order/addAssistPage.html",
        data:{
            id:id
        },
        success: function (resHtml) {
            if(resHtml.msg == null){//后台没有异常

                assistIndex = dggui.dialog.open({
                    title:'添加协单人',
                    content: resHtml,
                    area: '500px',

                    //弹框之后回调函数
                    success:function(layero,index){
                        dgg.dialog.relocation(index);//弹框重新布局
                        dggui.dialog.loading('close')
                    },
                    //点击确定触发
                    yes: function (index, layero) {
                        var assistIds = getVal('#assistId',0);//获取select选中的id

                            dggui.dialog.loading();
                            $.ajax({
                                type: 'POST',
                                data: {assistIds: assistIds, productOrderId: id},
                                url: sysInfo.basePath + "/rzsc/processing_order/updateAssist.do",
                                dataType: 'JSON',
                                success: function (data) {
                                    dggui.dialog.loading('close')
                                    if(data.code == 0 ){//保存成功
                                        dggui.dialog.succeed(data.msg,2000);
                                        layer.close(index);//关闭弹框
                                        if(isEmpty(tableName)){
                                            window.location.reload();
                                        }else{
                                            reloadTable($('#inProcess'),"reloadFalse");
                                        }
                                    }else{
                                        dggui.dialog.errors(data.msg);
                                    }
                                },
                                error: function () {
                                    dggui.dialog.loading('close')
                                    dggui.dialog.errors("发生未知错误，请联系管理员");
                                }
                            });

                    }//yes end
                })//open end
            }else {
                dggui.dialog.errors(resHtml.msg);
                dggui.dialog.loading('close')
            }
        }//success end
    })//ajax end

}

/*----------------更新渠道------------------------------*/
var channelIndex;
function updateChannel(tableName,scProductId) {
    var id = null;
    if(!isEmpty(tableName)){
        //获取选中行对象
        var trs = getChecked("#" + tableName);
        if (trs.length > 1) {
            dggui.dialog.errors('只能选择一个订单')
            return;
        }
        if (trs.length == 0) {
            dggui.dialog.errors('请选择一条数据');
            return;
        }
        id = trs[0].id;
    }else{
        if(isEmpty(scProductId)){
            dggui.dialog.errors('生产单ID不能为空');
            return;
        }
        id = scProductId;
    }

    dggui.dialog.loading();
    $.ajax({
        type: 'POST',
        async: false,
        url: sysInfo.basePath + "/rzsc/processing_order/update_channel.html",
        data:{
            id:id
        },
        success: function (resHtml) {
            if(resHtml.msg == null){//后台没有异常

                channelIndex = dggui.dialog.open({
                    title:'更新渠道',
                    content: resHtml,
                    area: '500px',

                    //弹框之后回调函数
                    success:function(layero,index){
                        dgg.dialog.relocation(channelIndex);//弹框重新布局
                        dggui.dialog.loading('close')
                    },

                    //点击确定触发
                    yes: function (index, layero) {
                        var id  = $("#currentId").val();//当前主订单id
                        var channelId = $("#channel option:selected").val();///渠道id
                        var subBranch='';
                        if(!strLength($("#updateSubBranch"),50,"支行名称长度不要超过50")){
                            subBranch=null;
                            return;
                        }else{
                            subBranch =$("#updateSubBranch").val().trim();//支行
                        }

                        dggui.dialog.loading();
                        $.ajax({
                            type: 'POST',
                            data: {id: id, channelId: channelId,subBranch:subBranch},
                            url: sysInfo.basePath + "/rzsc/processing_order/update_current_channel.do",
                            dataType: 'JSON',
                            success: function (data) {
                                dggui.dialog.loading('close')
                                if(data.code == 0 ){//保存成功
                                    dggui.dialog.succeed(data.msg,2000);
                                    layer.close(index);//关闭弹框
                                    if(isEmpty(tableName)){
                                        window.location.reload();
                                    }else{
                                        reloadTable("#" + tableName,"reloadFalse");
                                    }
                                }else{
                                    dggui.dialog.errors(data.msg);
                                }
                            },
                            error: function () {
                                dggui.dialog.loading('close')
                                dggui.dialog.errors("发生未知错误，请联系管理员");
                            }
                        });

                    }//yes end
                })//open end
            }else {
                dggui.dialog.errors(resHtml.msg);
                dggui.dialog.loading('close')
            }
        }//success end
    })//ajax end
}


/*----------------新增渠道------------------------------*/
var addChannelIndex;
function addChannel(tableName,scProdectId) {
    var id = null;
    if(!isEmpty(tableName)){
        //获取选中行对象
        var trs = getChecked("#" + tableName);
        if (trs.length > 1) {
            dggui.dialog.errors('只能选择一个订单')
            return;
        }
        if (trs.length == 0) {
            dggui.dialog.errors('请选择一条数据');
            return;
        }
        id =trs[0].id;
    }else{
        if(isEmpty(scProdectId)){
            dggui.dialog.errors('生产单ID不能为空');
            return;
        }
        id = scProdectId;
    }

    dggui.dialog.loading();

    $.ajax({
        type: 'POST',
        async: false,
        url: sysInfo.basePath + "/rzsc/processing_order/add_channel_html.html",
        data:{
            id:id
        },
        success: function (resHtml) {
            if(resHtml.msg == null){//后台没有异常

                addChannelIndex = dggui.dialog.open({
                    title:'新增渠道',
                    content: resHtml,
                    area: '500px',
                    //弹框之后回调函数
                    success:function(layero,index){
                        dgg.dialog.relocation(addChannelIndex);//弹框重新布局
                        dggui.dialog.loading('close')
                    },

                    //点击确定触发
                    yes: function (index, layero) {
                        var id  = $("#currentId").val();//当前主订单id

                        var productId = $("#currentProductId").val();///产品id
                        var channelId = $("#channel option:selected").val();///渠道id
                        var channelName = $("#channel option:selected").text();///渠道名称
                        var processUserId = $("#addFlowUserId option:selected").val();///流程人员id
                        var channelSubBranch=$("#channelSubBranch").val().trim();//支行
                        var contractNo = $("#contractNo").val();///合同编号
                        var amount = $("#money").val();///金额
                        var flag  = true;
                        if(isEmpty(productId)){
                            dggui.dialog.tip('请选择产品',$("#channel"),{tipsMore: true})
                           return;
                        }

                        if(isEmpty(channelId)){
                            dggui.dialog.tip('请选择渠道',$("#channelTip"),{tipsMore: true})
                            return;
                        }

                        if(isEmpty(processUserId)){
                            dggui.dialog.tip('请选择流程人员',$("#addFlowUserTip"),{tipsMore: true})
                            return;
                        }
                        if(!isEmpty(channelSubBranch) && channelSubBranch.length>50){
                            dggui.dialog.tip('支行信息最多50个字',$("#channelSubBranch"),{tipsMore: true})
                            return;
                        }

                        //验证金额
                        if(isEmpty(amount)){
                            dggui.dialog.tip('请输入金额',$("#money"),{tipsMore: true})
                           return;
                        }else{

                            if(!isNumberTip($("#money"),"请输入正确的数字","定金不能超过99999999999")){
                                return;
                            }
                        }

                       /* if(!flag){//没有通过验证结束方法
                            return;
                        }*/

                        dggui.dialog.loading();
                        $.ajax({
                            type: 'POST',
                            data: {id: id, channelId: channelId,channelName:channelName, processUserId: processUserId,contractNo:contractNo,amount:amount,subBranch:channelSubBranch},
                            url: sysInfo.basePath + "/rzsc/processing_order/add_channel.do",
                            dataType: 'JSON',
                            success: function (data) {
                                dggui.dialog.loading('close')
                                if(data.code == 0 ){//保存成功
                                    dggui.dialog.succeed(data.msg,2000);
                                    layer.close(index);//关闭弹框
                                    if(isEmpty(tableName)){
                                        window.location.reload();
                                    }else{
                                        reloadTable("#"+tableName,"reload");//刷新分页
                                    }
                                }else{
                                    dggui.dialog.errors(data.msg);
                                }
                            },
                            error: function () {
                                dggui.dialog.loading('close')
                                dggui.dialog.errors("发生未知错误，请联系管理员");
                            }
                        });

                    }//yes end
                })//open end
            }else {
                dggui.dialog.errors(resHtml.msg);
                dggui.dialog.loading('close')
            }
        }//success end
    })//ajax end
}


